package com.tj.admin.rest.product;

import com.alibaba.fastjson.JSON;
import com.fqgj.base.services.redis.RedisClient;
import com.tj.bo.common.conf.api.ApiResponse;
import com.tj.bo.common.conf.constant.RedisKey;
import com.tj.bo.common.conf.util.HttpUtil;
import com.tj.bo.product.client.ProductService;
import com.tj.bo.product.client.vo.Product;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * User: yujianfu (mr.vencnet@gmail.com)
 * Date: 2020-08-20
 * Time: 14:07
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProductController.class);

    @Autowired
    private ProductService productService;


    @Autowired
    private RedisClient redisClient;


    @RequestMapping("/updateStatus")
    public ApiResponse<String> updateProduct(@RequestBody ProductUpdateRequest request) {

        String startHour = request.getStartHour();
        if (!startHour.endsWith(":00")) {
            startHour += ":00";
        }

        String endHour = request.getEndHour();
        if (!endHour.endsWith(":00")) {
            endHour += ":00";
        }

        Product product = new Product()
                .setDefaultShow(Integer.valueOf(request.getIsDefaultShow()))
                .setShowFlag(request.getIsShow().equals("1"))
                .setLocked(request.getIsLock().endsWith("1"))
                .setCurrentOdds(request.getCurrentOdds())

                .setMiniOdds(request.getMiniOdds())
                .setOdds(request.getOdds())
                .setOpeningTime(startHour)
                .setClosingTime(endHour);

        LOGGER.info("==更新产品信息==,product:{}", JSON.toJSONString(product));
        boolean b = productService.updateProductBasicInfoById(request.getId(), product);
        if (b) {
            redisClient.del(RedisKey.BINARY_OPTION_PRODUCT_CACHE_KEY);
            redisClient.del(RedisKey.BINARY_OPTION_PRODUCT_ODDS_CONFIG_KEY);
            return new ApiResponse<>("update success");
        } else {
            return new ApiResponse<>("failed");
        }
    }

    public static void main(String[] args) {
        String page = HttpUtil.getPage("https://app-api.abctradeapp.com/deleteCache/A111222/A111222");

        System.err.println(page);

    }
}
